package com.suixingfu.service;

import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.suixingfu.utils.Base64Utils;
import com.suixingfu.utils.DESUtils;
import com.suixingfu.utils.JsonUtils;
import com.suixingfu.utils.PublicPartUtils;
import com.suixingfu.utils.RSAUtils;
import com.suixingfu.vo.RequestMessage;


public class DaiFuChaXun {
	public static String getDaifuJiaoyiAjax(JSONObject json1) throws UnsupportedEncodingException, Exception{
		// TODO Auto-generated method stub
//		String json ="{'clientId':'836100058122636','version':'1.0','reqId':'2016122516091210011','tranCd':'DF1005','reqData':{'stDate':'20160811','pageSize':'100','pageNumber':'1'}}";
		//String json = "{'clientId':'836100058122636','version':'1.0','reqId':'2016122516091210001','tranCd':'DF1002','reqData':{}}";// 客户请求的json数据
		String json=JSONObject.toJSONString(json1);
		System.out.println("json="+json);
		RequestMessage erm = JsonUtils.fromJson(json, RequestMessage.class);

		String reqData = erm.getReqData();

		// DES密钥加密
		String DESkey = PublicPartUtils.DESkey;
		byte[] bs = DESUtils.encrypt(reqData.getBytes("UTF-8"), DESkey);
		// 编码
		String reqDataEncrypt = Base64Utils.encode(bs);
		erm.setReqData(reqDataEncrypt);

		// 合作商私钥签名
		String RSAKey = PublicPartUtils.RSAKey;
		erm.setSign(RSAUtils.sign(reqDataEncrypt, RSAKey));

		System.out.println(JsonUtils.toJson(erm));
		String jstr = JsonUtils.toJson(erm);
		String url = PublicPartUtils.url+"paygateway/querySettleDtl.do";

		URL urlt = new URL(url);
		URLConnection urlConnection = urlt.openConnection();
		// 设置doOutput属性为true表示将使用此urlConnection写入数据
		urlConnection.setDoOutput(true);
		// 定义待写入数据的内容类型，我们设置为application/x-www-form-urlencoded类型
		urlConnection.setRequestProperty("content-type", "application/json;charset=utf-8");
		urlConnection.setRequestProperty("accept", "application/json;chaeset=utf-8");
		// 得到请求的输出流对象
		OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
		// 把数据写入请求的Body
		// JSONObject reqheadj = JSONObject.fromObject(reqhead);
		out.write(jstr);
		out.flush();
		out.close();

		// 从服务器读取响应
		InputStream inputStream = urlConnection.getInputStream();
		String encoding = urlConnection.getContentEncoding();
		String body = IOUtils.toString(inputStream, encoding);
		System.out.println(body);
		
		String Rjson=body;//随行付返回的json数据
		
		RequestMessage requestMessage = JsonUtils.fromJson(Rjson,RequestMessage.class);

		String sign = requestMessage.getSign();
		String resData = requestMessage.getResData();
		
		if(resData==null){
			resData="";
		}

		// 验证签名，随行付公钥
		String publicKey = PublicPartUtils.publicKey;
		boolean signFlag = sign !=null && StringUtils.isNotBlank(resData) && RSAUtils.verify(resData, sign, publicKey);
		if (!signFlag)
			System.out.println("签名验证失败");//根据实际业务修改

		// 解密
		byte[] base64bs = Base64Utils.decode(resData);
		
		// DES解密
		String DesKey=PublicPartUtils.DESkey;
		byte[] debs = DESUtils.decrypt(base64bs, DesKey);
		
		String resDataDecrypt = new String(debs,"UTF-8");
		
		requestMessage.setResData(resDataDecrypt);
		
		System.out.println(JsonUtils.toJson(requestMessage));
		
		return JsonUtils.toJson(requestMessage);

		
	}
	
//	public static String getDaifuJiaoyi() throws UnsupportedEncodingException, Exception{
//		// TODO Auto-generated method stub
//		String json ="{'clientId':'836100058122636','version':'1.0','reqId':'2016122516091210011','tranCd':'DF1005','reqData':{'stDate':'20160811','pageSize':'100','pageNumber':'1'}}";
//		//String json = "{'clientId':'836100058122636','version':'1.0','reqId':'2016122516091210001','tranCd':'DF1002','reqData':{}}";// 客户请求的json数据
//		System.out.println("json="+json);
//		RequestMessage erm = JsonUtils.fromJson(json, RequestMessage.class);
//
//		String reqData = erm.getReqData();
//
//		// DES密钥加密
//		String DESkey = PublicPartUtils.DESkey;
//		byte[] bs = DESUtils.encrypt(reqData.getBytes("UTF-8"), DESkey);
//		// 编码
//		String reqDataEncrypt = Base64Utils.encode(bs);
//		erm.setReqData(reqDataEncrypt);
//
//		// 合作商私钥签名
//		String RSAKey = PublicPartUtils.RSAKey;
//		erm.setSign(RSAUtils.sign(reqDataEncrypt, RSAKey));
//
//		System.out.println("erm="+JsonUtils.toJson(erm));
//		String jstr = JsonUtils.toJson(erm);
//		String url = "http://106.37.197.53:38080/paygateway/querySettleDtl.do";
//
//		URL urlt = new URL(url);
//		URLConnection urlConnection = urlt.openConnection();
//		// 设置doOutput属性为true表示将使用此urlConnection写入数据
//		urlConnection.setDoOutput(true);
//		// 定义待写入数据的内容类型，我们设置为application/x-www-form-urlencoded类型
//		urlConnection.setRequestProperty("content-type", "application/json;charset=utf-8");
//		urlConnection.setRequestProperty("accept", "application/json;chaeset=utf-8");
//		// 得到请求的输出流对象
//		OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
//		// 把数据写入请求的Body
//		// JSONObject reqheadj = JSONObject.fromObject(reqhead);
//		out.write(jstr);
//		out.flush();
//		out.close();
//
//		// 从服务器读取响应
//		InputStream inputStream = urlConnection.getInputStream();
//		String encoding = urlConnection.getContentEncoding();
//		String body = IOUtils.toString(inputStream, encoding);
//		System.out.println("body="+body);
//		
//		String Rjson=body;//随行付返回的json数据
//		
//		RequestMessage requestMessage = JsonUtils.fromJson(Rjson,RequestMessage.class);
//
//		String sign = requestMessage.getSign();
//		String resData = requestMessage.getResData();
//		
//		if(resData==null){
//			resData="";
//		}
//		
//		// 验证签名，随行付公钥
//		String publicKey = PublicPartUtils.publicKey;
//		boolean signFlag = sign !=null && StringUtils.isNotBlank(resData) && RSAUtils.verify(resData, sign, publicKey);
//		if (!signFlag)
//			System.out.println("签名验证失败");//根据实际业务修改
//
//		// 解密
//		byte[] base64bs = Base64Utils.decode(resData);
//		
//		// DES解密
//		byte[] debs = DESUtils.decrypt(base64bs, "12345678");
//		
//		String resDataDecrypt = new String(debs,"UTF-8");
//		
//		requestMessage.setResData(resDataDecrypt);
//		
////		System.out.println(JsonUtils.toJson(requestMessage));
//		
//		return JsonUtils.toJson(requestMessage);
//
//	}
}
